﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.IO;
    using System.Windows;

    /// <summary>
    /// Interaction logic for EditVideoWindow.xaml
    /// </summary>
    public partial class EditVideoWindow : Window
        {

        private bool editing = false;

        public VideoServiceItem Video { get; set; }

        public EditVideoWindow(VideoServiceItem existingVideo = null)
            {
            InitializeComponent();
            this.Video = existingVideo;
            this.editing = existingVideo != null;
            if (existingVideo != null)
                {
                this.TitleTextBox.Text = existingVideo.Title;
                this.OpenFileControl.FileName = existingVideo.FileName;
                }
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a title has been given
                if (string.IsNullOrEmpty(TitleTextBox.Text))
                    {
                    throw new Exception("Please give a title to this video item so that you can identify it in the play list.");
                    }

                // Ensure a file name has been specified, and that the file exists
                if (string.IsNullOrEmpty(OpenFileControl.FileName) || !File.Exists(OpenFileControl.FileName))
                    {
                    throw new Exception("Please specify the file name of an existing video file.");
                    }

                // Compose a video service item object
                if (Video == null) this.Video = new VideoServiceItem();
                Video.Title = TitleTextBox.Text;
                Video.FileName = OpenFileControl.FileName;

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Error Editing Video Details", ex.Message);
                }

            }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
            {
            try
                {
                var fileName = new Uri(OpenFileControl.FileName);
                Media.Source = fileName;
                Media.Play();
                }
            catch { }
            }

        private void StopButton_Click(object sender, RoutedEventArgs e)
            {
            Media.Stop();
            Media.Close();
            }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
            StopButton_Click(null, null);
            }

        private void PartialCheckBoxChanged(object sender, RoutedEventArgs e)
            {
            FromTextBox.IsEnabled = PartialCheckBox.IsChecked == true;
            ToTextBox.IsEnabled = PartialCheckBox.IsChecked == true;
            }

        private void OpenFileControl_FileNameChanged(object sender, EventArgs e)
            {
            try
                {
                this.TitleTextBox.Text = Path.GetFileNameWithoutExtension(OpenFileControl.FileName);
                }
            catch { }
            }

        }

    }
